package cn.nubia.admin.application.service;

import cn.nubia.admin.application.bean.ShareTemplate;
import cn.nubia.admin.application.query.ShareTemplateQuery;
import cn.nubia.admin.common.mybatis.bean.Paged;
import cn.nubia.admin.common.mybatis.bean.Query;
import cn.nubia.admin.common.mybatis.service.BaseServiceImpl;
import cn.nubia.admin.common.springmvc.WebContext;
import cn.nubia.admin.common.utils.DateUtil;
import cn.nubia.admin.sysconfig.bean.DictField;
import cn.nubia.admin.sysconfig.bean.DictFieldItem;
import cn.nubia.admin.sysconfig.bean.SystemLog;
import cn.nubia.admin.sysconfig.service.DictFieldItemService;
import cn.nubia.admin.sysconfig.service.SystemLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * TemplateService
 *
 * @author nb
 * @date 2015/7/14
 * @since V1.0
 */
@Service
public class ShareTemplateService extends BaseServiceImpl<ShareTemplate>{
    @Autowired
    private DictFieldItemService dictFieldItemService;

    @Autowired
    SystemLogService systemLogService;


    /**
     * 保存模板
     * @param shareTemplate
     */
    public void save(ShareTemplate shareTemplate) {
        Integer adminId = WebContext.getCachedUser().getId();//WebContext.getCachedUser().getId()
        String  adminName = WebContext.getCachedUser().getName();//WebContext.getCachedUser().getName()
        Integer curr = DateUtil.getCurrentTime();
        SystemLog.OperateTypeEnum type = SystemLog.OperateTypeEnum.ADD;
        shareTemplate.setLastModifyTime(curr);
        if(shareTemplate.getId()==null){
            shareTemplate.setCreateTime(DateUtil.getCurrentTime());
            shareTemplate.setCreateId(WebContext.getCachedUserId());
            shareTemplate.setCreateName(WebContext.getCachedUser().getName());
            type = SystemLog.OperateTypeEnum.ADD;
        }else {
            ShareTemplate domain = get(shareTemplate.getId());
            shareTemplate.setCreateTime(domain.getCreateTime());
            shareTemplate.setCreateId(domain.getCreateId());
            shareTemplate.setCreateName(domain.getCreateName());
            shareTemplate.setLastModifyUser(WebContext.getCachedUser().getName());
            type = SystemLog.OperateTypeEnum.EDIT;
        }
        this.getDAO().save(shareTemplate);

        //log
        systemLogService.log(SystemLog.ModuleTypeEnum.SEARCHGENERALLIST, type, shareTemplate.getId());
    }

    /**
     * 查看模板
     * @param id
     * @return
     */
    public ShareTemplate viewShareTemplat(Integer id) {
        return get(id);
    }

    /**
     * 模板列表
     * @param shareTemplateQuery
     * @return
     */
    public Paged<ShareTemplate> list(ShareTemplateQuery shareTemplateQuery) {
        Query query = Query.build(ShareTemplate.class);
        query.addLike("templateName",shareTemplateQuery.getTemplateName());
        query.addEq("templateType",shareTemplateQuery.getTemplateType());
        query.setPaged(shareTemplateQuery.getPageNo(),shareTemplateQuery.getPageSize());
        query.addOrder("id", Query.DBOrder.DESC);
        Paged<ShareTemplate> paged =findPagedByQuery(query);
        for(ShareTemplate shareTemplate:paged.getListData()){
         DictFieldItem dictFieldItem=  dictFieldItemService.get(shareTemplate.getTemplateType());
            shareTemplate.setDictFieldItem(dictFieldItem.getItemTitle());
        }
        return paged;
    }

    public List<DictFieldItem> getTemplateTypeList() {
        return dictFieldItemService.listItemsByDictName(DictField.DICTFIELDITEM_TEMPLATE_NAME);
    }
}
